{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "19a41bcb",
   "metadata": {},
   "source": [
    "## numpy axis轴的理解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "42930c0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "890b9015",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2],\n",
       "        [ 3,  4,  5]],\n",
       "\n",
       "       [[ 6,  7,  8],\n",
       "        [ 9, 10, 11]],\n",
       "\n",
       "       [[12, 13, 14],\n",
       "        [15, 16, 17]]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.arange(18).reshape(3,2,3)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e74c8bdb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据的维度\n",
    "data.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4124352b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3, 3)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d92d3288",
   "metadata": {},
   "source": [
    "### 1.数组最外面的括号代表axis=0,依次往里面的括号对应的axis的计数依次加1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9aad2940",
   "metadata": {},
   "source": [
    "### 2.针对数组的函数操作，针对轴下一级的直接子原组分别进行操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e4b71981",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [2, 3]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1 = np.arange(4).reshape(2,2)\n",
    "data1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fd49e10",
   "metadata": {},
   "source": [
    "## 3.相关函数的实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1d34ad9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#如果axis=0时，数据根据总数组下一级[0,1]\\[2,3]中的第一个元素分别相加，第二个元素分别相加，最后得出结果。\n",
    "data1.sum(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9a34278c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 5])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#如果指定axis=1则，[0,1]下面的数组(元素)即0,1两个相加得出结果，[2,3]中的两个元素相加得出结果。\n",
    "data1.sum(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "30dbe1f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#max函数,axis=0\n",
    "data1.max(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "30ab5830",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#max函数,axis=1\n",
    "data1.max(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "7e45b30e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 8, 8, 2, 1],\n",
       "       [0, 0, 0, 4, 8],\n",
       "       [0, 4, 4, 1, 1]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#delete函数的用法\n",
    "data2 = np.random.randint(0,9,size=(3,5))\n",
    "data2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd1fece6",
   "metadata": {},
   "source": [
    "* delete函数删除数组中指定的axis中的本层数组。\n",
    "* 此处与其他函数不同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "fa6a7e06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 8, 2, 1],\n",
       "       [0, 0, 4, 8],\n",
       "       [4, 4, 1, 1]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.delete(data2,0,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "11c6634d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 8, 8, 2, 1],\n",
       "       [0, 0, 0, 4, 8],\n",
       "       [0, 4, 4, 1, 1]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
